package com.foreverwin.mes.warehouse.serviceext;

import com.foreverwin.mes.warehouse.constant.Constants;
import com.sap.me.activity.ActivityRuntimeType;
import com.sap.me.activity.ExecutionType;
import com.sap.me.activity.ServiceExtension;
import com.sap.me.inventory.InventoryDataField;
import com.sap.me.inventory.InventoryValidateAndUpdateRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * MES标准服务扩展开发样例
 *
 * @author Ervin Chen
 * @date 2020/2/25 21:18
 */
@ExecutionType(ActivityRuntimeType.SYNC)
public class InventoryValidateAndUpdateExtension extends ServiceExtension<Object> {
    private final Logger logger = LoggerFactory.getLogger(InventoryValidateAndUpdateExtension.class);

    @Override
    public void execute(Object request) throws Exception {
        if (request instanceof InventoryValidateAndUpdateRequest) {
            execute((InventoryValidateAndUpdateRequest) request);
        }
    }

    public void execute(InventoryValidateAndUpdateRequest validateAndUpdateRequest) throws Exception {
        logger.info("inventory validate and update service extension start");

        List<InventoryDataField> assemblyDataFields = validateAndUpdateRequest.getAssemblyDataList();
        for (InventoryDataField field : assemblyDataFields) {
            if (Constants.CENTRAL_STORAGE_LOCATION.equals(field.getAttribute())) {
                field.setValue("1001");
                break;
            }
        }
    }

}
